class Admins::NotifiesController < ApplicationController
  before_action :set_notify, only: [:show, :edit, :update, :destroy,:submit,:approve,:reject]
  before_action :set_edit_data,only: [:new,:edit,:update,:create]
  
  # GET /notifies
  # GET /notifies.json
  def index
    notify_type = params[:notify_type] || '1'
     session[:department] = params[:department] if params[:department]
    session[:news_begin] = params[:news_begin] if params[:news_begin]
    session[:news_end] = params[:news_end] if params[:news_end]
    session[:notify_id] = params[:notify_id] if params[:notify_id]
    session[:notify_type] = params[:notify_type] if params[:notify_type]
    @current_user = current_user
    @departments = Department.all
    @notifies = Notify.notify_type(notify_type,current_user).check(params[:check])
       .pubtime(params[:news_begin] || session[:news_begin],params[:news_end] || session[:news_end])
       .contribution_units_id(params[:department])
       .notify_id(params[:notify_id] || session[:notify_id])
       .page(params[:page])
   

 
       
  end

  # GET /notifies/1
  # GET /notifies/1.json
  def show
  end

  # GET /notifies/new
  def new
    @notify = Notify.new
  end

  # GET /notifies/1/edit
  def edit
  end

  # POST /notifies
  # POST /notifies.json
  def create
    @notify = Notify.new(notify_params.merge({postby: current_user.name}))
    files = params[:files]
    (@notify.extra_files = save_file_for_notify files) unless files.nil?
    respond_to do |format|
      if @notify.pubtime.nil?
        @notify.pubtime = Time.now
      end
      if @notify.save
        format.html { redirect_to admins_notifies_path, notice: '通告创建成功.' }
        format.json { render :show, status: :created, location: @notify }
      else
        flash[:error] = @notify.errors.full_messages.join(",")
        format.html { render :new }
        format.json { render json: @notify.errors, status: :unprocessable_entity }
      end
    end
  end

  def submit
    respond_to do |format|
      if @notify.update_attributes(check: 1)
        format.html { redirect_to admins_notifies_path @notify, notice: '通告送审成功.' }
        format.json { render :show, status: :ok, location: @notify }
      else
        format.html { render :edit }
        format.json { render json: @notify.errors, status: :unprocessable_entity }
      end
    end
  end

  #采用
  def approve
    @notify.update_attributes(check: 2)
    redirect_to admins_notifies_path @notify, notice: '通告采用成功.'
  end

  #不采用
  def reject
    @notify.update_attributes(check: 3,reason: params[:reason])
    redirect_to admins_notifies_path @notify, notice: '通告退回成功.'
  end

  # PATCH/PUT /notifies/1
  # PATCH/PUT /notifies/1.json
  def update
      respond_to do |format|
      files = params[:files]
         
      if @notify.update(notify_params)
          
        filestr =  save_file_for_notify files unless files.nil?
        extra_filestr = @notify.extra_files 

        if extra_filestr=='' || extra_filestr.nil?
          extra_filestr = '';
          extra_filestr +=filestr unless files.nil?
        else
          extra_filestr +="&,&" +filestr unless files.nil?
        end
        @notify.update_attributes(:extra_files=>(extra_filestr))
        format.html { redirect_to admins_notifies_path, notice: '通告修改成功' }
        format.json { render :show, status: :ok, location: @notify }
      else
        format.html { render :edit }
        format.json { render json: @notify.errors, status: :unprocessable_entity }
      end
    end
  end
  
  
  
  def change_time
    id = params[:id]
    time =params[:new_time]
    new =Notify.find_by_id id
    if new
      if new.pubtime
        new.update_attributes(pubtime:Time.parse(time))
      else
        new.update_attributes(created_at:Time.parse(time))
      end
    end
    redirect_to '/admins/notifies'
  end
  # DELETE /notifies/1
  # DELETE /notifies/1.json
  def destroy
    @notify.destroy
    respond_to do |format|
      format.html { redirect_to admins_notifies_url, notice: '通告删除成功' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_notify
      @notify = Notify.find(params[:id])
      p @notify.pubtime
      p @notify.deleted
      p '==================='
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def notify_params
      params.require(:notify).permit(:title, :content, :status, :deleted, :contribution_units, :contribution_units_id, :pubtime,:extra_files)
    end

  def save_file_for_notify files
    path_zip = "#{Rails.root.to_s}/public/UploadFile/accessory"
    file_url_string = []
    files.each do |f|
     filename="#{f.original_filename.split(".")[0]}#{Date.today.to_s.gsub("-","")}#{(100+rand(899)).to_s}.#{f.original_filename.split(".")[1]}"
      full_path=path_zip+"/#{filename}"
      file_url = "/UploadFile/accessory/#{filename}"
      file_url_string << file_url
      svae_afile(f,full_path,path_zip)
    end
    file_url_string.join("&,&")
  end



  ######### 保存一个文件
  def svae_afile(tmp,full_path,path_zip)
    FileUtils.mkdir_p path_zip unless File.exists?(path_zip)
    save tmp,full_path
  end
  #保存单个文件
  def save tmp,full_path
    #file = File.join("public",@tmp.original_filename)
    #dirname=Rails.root.to_s+SITE_PATH % @root_path+"//resources"
    file1=File.new(full_path,'wb')
    FileUtils.cp tmp.path,file1

  end

  def set_edit_data
    @departments = Department.all
  end
end
